package nc.files.ct.utils;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;

import nc.bs.framework.common.NCLocator;
import nc.itf.uap.IUAPQueryBS;
import nc.jdbc.framework.SQLParameter;
import nc.jdbc.framework.processor.ColumnProcessor;
import nc.vo.pub.BusinessException;

@SuppressWarnings("restriction")
public class RequestTools {

	public static String readInput(HttpServletRequest request) {
		ServletInputStream sis = null;
		try {
			sis = request.getInputStream();
			int contentLength = request.getContentLength();
			byte[] buffer = new byte[contentLength];
			byte[] inputByte = new byte[contentLength];
			int count = 0;
			int readbyte = 0;
			while (count < contentLength) {
				readbyte = sis.read(buffer);
				for (int i = 0; i < readbyte; i++) {
					inputByte[count + i] = buffer[i];
				}
				count += readbyte;
			}

			String postString = new String(inputByte, "UTF-8");
			return postString;
		} catch (Exception e) {
			return null;
		} finally {
			if (sis != null) {
				try {
					sis.close();
				} catch (Exception ce) {

				}
			}
		}

	}
	
	public static boolean isSyskeyOK(String exSyscode, String exSyskey) {
		boolean isOK = false;
		if (exSyscode != null && exSyskey != null) {
			IUAPQueryBS query = NCLocator.getInstance().lookup(IUAPQueryBS.class);
			String sql = "select exsystemcode from xx_exsystem where exsystemcode=? and exsystemname2=?";
			SQLParameter param = new SQLParameter();
			param.addParam(exSyscode);
			param.addParam(exSyskey);
			try {
				String dbExSyscode = (String) query.executeQuery(sql, param, new ColumnProcessor());
				if (exSyscode.equals(dbExSyscode)) {
					isOK = true;
				}
			} catch (BusinessException e) {
				e.printStackTrace();
			}
		}
		return isOK;
	}

}
